<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //配合Proxy实现更好的拦截
        let s = new Set()
        let proxy = new Proxy(s, {
            get(target, key) {
                //判断如果是方法，就要修正this指向
                let value = Reflect.get(target, key)
                if (value instanceof Function) {
                    return value.bind(target) //将this指向target，target也就是s
                }
                return value
            },
            set(target, key, value) {
                //走原对象的默认行为即可
                Reflect.set(...arguments)
            }
        })
        //拦截数组，非常方便！
        let arr = [1, 2, 3]
        let proxy2 = new Proxy(arr, {
            get(target, key) {
                console.log("get", key)
                return Reflect.get(...arguments)
            },
            set(target, key, value) {
                console.log("set", key, value)
                return Reflect.set(...arguments)
            }
        })
        proxy2.push(4)
    </script>
</body>

</html>